package com.dd.common.entity;

import com.dd.common.IErrorCode;
import io.swagger.annotations.ApiModel;
import io.swagger.annotations.ApiModelProperty;

@ApiModel(
        value = "CommonResult",
        description = "后端返回结果"
)
public class CommonResult<T> {
    @ApiModelProperty(value = "返回结果编码", name = "code")
    private long code;
    @ApiModelProperty(
            value = "返回结果信息",
            name = "message"
    )
    private String message;
    @ApiModelProperty(
            value = "返回结果数据",
            name = "data"
    )
    private T data;

    protected CommonResult() {
    }

    public CommonResult(long code, String message) {
        this.code = code;
        this.message = message;
    }

    public CommonResult(long code, String message, T data) {
        this.code = code;
        this.message = message;
        this.data = data;
    }

    public static <T> CommonResult<T> success(T data) {
        return new CommonResult((long) ResultCode.SUCCESS.getCode(), ResultCode.SUCCESS.getMessage(), data);
    }

    public static <T> CommonResult<T> success(T data, String message) {
        return new CommonResult((long) ResultCode.SUCCESS.getCode(), message, data);
    }

    public static <T> CommonResult<T> failed(IErrorCode errorCode) {
        return new CommonResult((long) errorCode.getCode(), errorCode.getMessage(), (Object) null);
    }

    public static <T> CommonResult<T> failed(String message) {
        return new CommonResult((long) ResultCode.FAILED.getCode(), message, (Object) null);
    }

    public static <T> CommonResult<T> failed() {
        return failed((IErrorCode) ResultCode.FAILED);
    }

    public static <T> CommonResult<T> validateFailed() {
        return failed((IErrorCode) ResultCode.VALIDATE_FAILED);
    }

    public static <T> CommonResult<T> validateFailed(String message) {
        return new CommonResult((long) ResultCode.VALIDATE_FAILED.getCode(), message, (Object) null);
    }

    public static <T> CommonResult<T> unauthorized(T data) {
        return new CommonResult((long) ResultCode.UNAUTHORIZED.getCode(), ResultCode.UNAUTHORIZED.getMessage(), data);
    }

    public static <T> CommonResult<T> accessDenied(T data) {
        return new CommonResult((long) ResultCode.FORBIDDEN.getCode(), ResultCode.FORBIDDEN.getMessage(), data);
    }

    public static <T> CommonResult<T> loginFail(T data) {
        return new CommonResult((long) ResultCode.UNAUTHORIZED.getCode(), ResultCode.UNAUTHORIZED.getMessage(), data);
    }

    public void setCode(final long code) {
        this.code = code;
    }

    public void setMessage(final String message) {
        this.message = message;
    }

    public void setData(final T data) {
        this.data = data;
    }

    public long getCode() {
        return this.code;
    }

    public String getMessage() {
        return this.message;
    }

    public T getData() {
        return this.data;
    }
}
